package com.lideru.backstage.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lideru.backstage.entity.BookType;
import com.lideru.backstage.mapper.BookTypeMapper;
import com.lideru.backstage.utils.JWTUtils;
import com.lideru.backstage.utils.common.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;


@RestController
@RequestMapping("/bookType")

public class BookTypeController {
    @Resource
    BookTypeMapper bookTypeMapper;

    @PostMapping
    public Result<?> save(@RequestBody BookType bookType ) {
        bookType.setGmtCreate(new Date());
        bookType.setGmtModified(new Date()); 
        int i = bookTypeMapper.insert(bookType);
        if (i<1){
            return Result.error("-1","新增书本类型失败！");
        }
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody BookType BookType) {
        BookType.setGmtModified(new Date());
        int i = bookTypeMapper.updateById(BookType);
        if (i<1){
            return Result.error("-1","修改书本类型失败！");
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id ) {
        BookType bookType = bookTypeMapper.selectOne(Wrappers.<BookType>lambdaQuery() 
                .eq(BookType::getId, id));
        if (bookType != null) {
            bookTypeMapper.deleteById(id);
            return Result.success();
        }
        return Result.error("403", "你没有权限");

    }

    @GetMapping("getAllBookType")
    public Result<?> getAllBookType(){
      return Result.success( bookTypeMapper.selectList(null))  ;
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search
                              ) {
        return Result.success(
                bookTypeMapper.selectPage(new Page<>(pageNum, pageSize), Wrappers.<BookType>lambdaQuery().like(BookType::getName, search)));
    }


}
